<?php

include_once('Database.php');
include_once('RestUtils.php');
include_once('model/Station.php');
include_once('model/Graph.php');

/**
 * Gets the statistical information for a station in JSON format
 *
 * $stationId	Id of the station to get information about
 * $firstData	Information on the x axis of the graph
 * $secondData	Information on the y axis of the graph
 */
function getStationStatistics($stationId, $firstData, $secondData)
{
	$db = new DBManager();
	$mysqli = $db->db_connect();

	$query = "SELECT stationId, $firstData, $secondData FROM StationInfo WHERE stationId=?";

	$stmt = $mysqli->prepare($query);
	$stmt->bind_param('i', $stationId);
	$stmt->execute();
	$result = $stmt->get_result();

	$data = array();

	while($row = $result->fetch_assoc())
	{
		//$data[] = array($row[$firstData], $row[$secondData]);
		$data[] = array($row[$secondData], $row[$firstData]);
	}


	$graph = new Graph($firstData, $secondData, $data);

	return json_encode($graph);
}

/**
 * Gets the all information for a station in JSON format
 *
 * $stationId	Id of the station to get information about
 */
function getStationInformation($stationId)
{
	$db = new DBManager();
	$mysqli = $db->db_connect();

	$query1 = "SELECT * FROM StationInfo WHERE stationId=?";

	$stmt1 = $mysqli->prepare($query1);
	$stmt1->bind_param('i', $stationId);
	$stmt1->execute();
	$result1 = $stmt1->get_result();

	$row1 = $result1->fetch_assoc();
	
	$station = new Station($row1['stationId'], $row1['currentTime'], $row1['numberOfPassenger'], $row1['distributionType'], $row1['distributionParams']);

	$query2 = "SELECT passengerId FROM Passenger WHERE destinationStationId=?";
	
	$stmt2 = $mysqli->prepare($query2);
	$stmt2->bind_param('i', $stationId);
	$stmt2->execute();
	$result2 = $stmt2->get_result();

	$list = array();

	while($row2 = $result2->fetch_assoc())
	{
		$list[] = $row2['passengerId'];
	}
	
	return json_encode(array($station, $list));
}

/**
 * Gets the list of stations in JSON format
 */
function getStationList()
{
	$db = new DBManager();
	$mysqli = $db->db_connect();

	$query = "SELECT stationId FROM Station";
	
	$stmt = $mysqli->prepare($query);
	$stmt->execute();
	$result = $stmt->get_result();

	$list = array();

	while($row = $result->fetch_assoc())
	{
		$list[] = $row['stationId'];
	}

	return json_encode($list);
}

/* Initialize the request
 *
 * Request format: PATH/Stations.php/stationId/firstData/secondData
 *
 * $requestData->getRequestVars() will have an array such that
 *
 * [3] will be stationId
 * [4] will be firstData
 * [5] will be secondData
 *
 * For more details about $requestData, see RestRequest class in RestUtils.php */
$requestData = RestUtils::processRequest();

/* Now let's handle the request */
switch($requestData->getMethod())
{
	/* If it was a GET request */
	case 'get':
		/* If there is a stationId */
		if(isset($requestData->getRequestVars()[4]))
		{
			/* If there is a firstData */
			if(isset($requestData->getRequestVars()[5]))
			{
				/* If there is a secondData */
				if(isset($requestData->getRequestVars()[6]))
				{
					/* At this point a graph with two specific data is wanted for a specific station */
					$result = getStationStatistics($requestData->getRequestVars()[4], $requestData->getRequestVars()[5], $requestData->getRequestVars()[6]);
				}
				else
				{
					/* At this point a graph with just one specific data is wanted for a specific station (the other data will be time as default)*/
					$result = getStationStatistics($requestData->getRequestVars()[4], $requestData->getRequestVars()[5], 'currentTime');
				}
			}
			else
			{
				/* At this point the all information is wanted for a specific station */
				$result = getStationInformation($requestData->getRequestVars()[4]);
			}
		}
		else
		{
			/* At this point the list of stations is wanted since no stationId is specified */
			$result = getStationList();
		}

		/* Echo the result */
		echo RestUtils::sendResponse(200, $result, 'application/json');
		break;

	/* No need to implement POST, PUT and DELETE requests yet */
}

?>